<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>08-光照</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    // 创建场景
    const scene = new THREE.Scene();
    // 创建相机
    const camera = new THREE.PerspectiveCamera(
      45,
      window.innerWidth / window.innerHeight,
      1,
      1000
    );
    camera.position.set(0, 20, 0);
    camera.lookAt(0, 0, 0);
    // 创建渲染器 
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    // 设置渲染器大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 把渲染的canvas内容添加到body
    document.body.appendChild(renderer.domElement);

    // 环境光
    const ambientLight = new THREE.AmbientLight(0xff0000, 1); // 柔和的白光
    // scene.add(ambientLight);

    // 平行光
    const directionalLight = new THREE.DirectionalLight(0xffffffff, 1);
    // scene.add(directionalLight);
    directionalLight.position.set(5, 5, 0);

    // 平行光辅助对象
    const directionalLightHelper = new THREE.DirectionalLightHelper(directionalLight);
    // scene.add(directionalLightHelper);
    
    // 半球光
    const hemisphereLight = new THREE.HemisphereLight(0xff0000, 0x0000ff, 1); 
    // scene.add(hemisphereLight);

    // 点光源
    const pointLight = new THREE.PointLight(0xff0000, 1, 100);
    pointLight.position.set(0, 5, 0);
    scene.add(pointLight);

    // 点光源辅助对象
    const pointLightHelper = new THREE.PointLightHelper(pointLight, 1);
    scene.add(pointLightHelper);

    const plane = new THREE.Mesh(
      new THREE.PlaneGeometry(10, 10),
      new THREE.MeshStandardMaterial()
    );
    plane.rotation.x = -Math.PI / 2;

    const cube = new THREE.Mesh(
      new THREE.BoxGeometry(2, 2, 2),
      new THREE.MeshStandardMaterial()
    );
    cube.position.set(0, 2, 0);

    const sphere = new THREE.Mesh(
      new THREE.SphereGeometry(1, 32, 32),
      new THREE.MeshStandardMaterial()
    );
    sphere.position.set(3, 2, 0);

    const torus = new THREE.Mesh(
      new THREE.TorusGeometry(1, 0.5),
      new THREE.MeshStandardMaterial()
    );
    torus.position.set(-3, 2, 0);


    scene.add(plane, cube, sphere, torus);


    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;

    // 监听controls的change事件
    controls.addEventListener("change", () => {
      // 交互的鼠标每次发生变化，都需要渲染一次(如果用了animate函数就不需要).
      renderer.render(scene, camera);
    });
    renderer.render(scene, camera);


    function animate() {
      requestAnimationFrame(animate);
      controls.update();
      renderer.render(scene, camera);
    } 

    // 添加动画并渲染
    animate();
  </script>
</body>

</html>